package com.zb.frame.modules.sys.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zb.frame.modules.sys.entity.RelUserRole;
import com.zb.frame.modules.sys.mapper.RelUserRoleMapper;
import com.zb.frame.modules.sys.service.RelUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bangZZ
 * @since 2020-06-04
 */
@Service
public class RelUserRoleServiceImpl extends ServiceImpl<RelUserRoleMapper, RelUserRole> implements RelUserRoleService {

    @Autowired
    RelUserRoleMapper userRoleMapper;

    @Override
    public List<RelUserRole> getUserInfoByRoleId(String roleId) {
        return userRoleMapper.getUserInfoByRoleId(roleId);
    }

    @Override
    public List<RelUserRole> getRoleInfoByUserId(Long userId) {
        return userRoleMapper.getRoleInfoByUserId(userId);
    }

    @Override
    public boolean addRelBatch(List<RelUserRole> list) {
        return this.saveBatch(list);
    }

    @Override
    public boolean deleteByRoleId(String roleId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_id", roleId);
        return this.remove(wrapper);
    }

    @Override
    public boolean deleteByUserIdAndRoleIds(Long userId, List<String> roleIds) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("roleIds", roleIds);
        return userRoleMapper.deleteByUserIdAndRoleIds(params) > 0;
    }

    /**
     * 获取用户角色列表
     * @param userId 用户主键
     * @return
     */
    @Override
    public List<String> getRoleIdsByUserId(Long userId) {
        List<String> ret = new ArrayList<>();
        QueryWrapper wrapper = new QueryWrapper().select("role_id");
        wrapper.eq("user_id", userId);
        List<RelUserRole> list = this.list(wrapper);
        for (RelUserRole obj : list) {
            ret.add(obj.getRoleId());
        }
        return ret;
    }


}
